//
//  RCPointsShopListCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-23.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCPointsShopListCell.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import "NSString+NSString_Ex_.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "UIImageView+WebCache.h"
#import "RCPointsShopListDataModel.h"

#define kDefaultimageFile        @"image_recommendFood_image_bg.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"
#define kLineImageFile           @"image_order_line.png"
#define kBGImageFile             @"image_discount_cell_bg.png"

@interface RCPointsShopListCell()

@property(nonatomic,retain)UIImageView *defaultImageView;
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel     *itemNameLabel;
@property(nonatomic,retain)UIImageView *startImageView1;
@property(nonatomic,retain)UIImageView *startImageView2;
@property(nonatomic,retain)UIImageView *startImageView3;
@property(nonatomic,retain)UIImageView *startImageView4;
@property(nonatomic,retain)UIImageView *startImageView5;
@property(nonatomic,retain)TTTAttributedLabel     *descLabel;
@property(nonatomic,retain)UILabel     *changeTimeLabel;
@property(nonatomic,retain)UILabel     *changePointLabel;
@property(nonatomic,retain)UIImageView *lineImageView;

@end

@implementation RCPointsShopListCell

@synthesize defaultImageView = _defaultImageView;
@synthesize picImageView = _picImageView;
@synthesize startImageView1 = _startImageView1;
@synthesize startImageView2 = _startImageView2;
@synthesize startImageView3 = _startImageView3;
@synthesize startImageView4 = _startImageView4;
@synthesize startImageView5 = _startImageView5;
@synthesize itemNameLabel = _itemNameLabel;
@synthesize descLabel = _descLabel;
@synthesize changeTimeLabel = _changeTimeLabel;
@synthesize changePointLabel = _changePointLabel;
@synthesize lineImageView = _lineImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self setBackgroundColorWithImage:bgImage];
        self.backgroundColor = [UIColor clearColor];
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.defaultImageView = nil;
    self.picImageView = nil;
    self.startImageView1 = nil;
    self.startImageView2 = nil;
    self.startImageView3 = nil;
    self.startImageView4 = nil;
    self.startImageView5 = nil;
    self.itemNameLabel = nil;
    self.descLabel = nil;
    self.changeTimeLabel = nil;
    self.changePointLabel = nil;
    self.lineImageView = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 16, 84, 66)];
    _defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultImageView setImage:defaultImage];
    [self addSubview:_defaultImageView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 19, 78, 60)];
    _picImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_picImageView];
    
    _itemNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 16, 110, 16)];
    _itemNameLabel.backgroundColor = [UIColor clearColor];
    _itemNameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _itemNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    [self addSubview:_itemNameLabel];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(231, 18, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView1];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(246, 18, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView2];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(261, 18, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView3];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(276, 18, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView4];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(291, 18, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView5];
    
    
    _descLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(110, 37, 196, 27)];
    _descLabel.backgroundColor = [UIColor clearColor];
    _descLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _descLabel.textColor = [UIColor colorWithHex:0xff505050];
    _descLabel.numberOfLines = 2;
    _descLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
    _descLabel.leading = 3;
    
    [self addSubview:_descLabel];
    
    _changeTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 70, 100, 12)];
    _changeTimeLabel.backgroundColor = [UIColor clearColor];
    _changeTimeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _changeTimeLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_changeTimeLabel];
    
    _changePointLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 70, 100, 12)];
    _changePointLabel.backgroundColor = [UIColor clearColor];
    _changePointLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _changePointLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_changePointLabel];
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 97, 320, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [self addSubview:_lineImageView];
    
    self.selectionStyle = UITableViewCellSelectionStyleGray;
}

-(void)bindData:(RCPointsShopListItem *)dataModel{
    if (nil == dataModel) {
        return;
    }
    if (![NSString stringIsNullOrEmpty:dataModel.itemPicUrl]) {
        [_picImageView setImageWithURL:[NSURL URLWithString:dataModel.itemPicUrl] placeholderImage:nil];
    }
    
    _itemNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.itemTitle]) {
        _itemNameLabel.hidden = NO;
        _itemNameLabel.text = dataModel.itemTitle;
    }
    
    NSInteger score = dataModel.itemScore;
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
    UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
    [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
    [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
    [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
    [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
    [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    
    _descLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.itemDesc]) {
        _descLabel.hidden = NO;
        _descLabel.text = dataModel.itemDesc;
    }
    
    _changeTimeLabel.hidden = YES;
    CGFloat width = 110;
    if (![NSString stringIsNullOrEmpty:dataModel.itemChangeTimes]) {
        _changeTimeLabel.hidden = NO;
        _changeTimeLabel.text = [NSString stringWithFormat:@"兑换次数：%@",dataModel.itemChangeTimes];
        CGSize size = [_changeTimeLabel.text sizeWithFont:_changeTimeLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect rect = _changeTimeLabel.frame;
        rect.size.width = size.width;
        _changeTimeLabel.frame = rect;
        
        width = CGRectGetMaxX(_changeTimeLabel.frame)+10;
    }
    
    _changePointLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.itemChangePoints]) {
        _changePointLabel.hidden = NO;
        _changePointLabel.text = [NSString stringWithFormat:@"兑换积分：%@",dataModel.itemChangePoints];
        
        CGRect rect = _changePointLabel.frame;
        rect.origin.x = width;
        _changePointLabel.frame = rect;
    }
    
}

+(CGFloat)getCellHeight{
    return 98;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
